﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "VimbaImageTransform/Include/VmbTransform.h"
#include <sstream>
#include <QDebug>
#include <QDir>
#include <QFileInfoList>
#include <QFileInfo>
#include <QSettings>
#include <QTextCodec>

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow),
	m_bIsCamera(false)
{
	ui->setupUi(this);
	RdConfigIni();
	setInterface();
	Initialize();

	SetCameraParameter();
	ShowSwitchBt();
	// 定时器
	myTimer = new QTimer(this);
	QObject::connect(myTimer, SIGNAL(timeout()), this, SLOT(systimer()));
	myTimer->start(1000);

	// 设置Label 双击放大事件
	ui->camera_1->installEventFilter(this);
	ui->camera_2->installEventFilter(this);
	ui->camera_3->installEventFilter(this);
	ui->camera_4->installEventFilter(this);
	ui->bigLbale->installEventFilter(this);


	// 获取检测参数
	SearchProFiles();
	RdProductIni(file_list[m_nProductNum]);
	SetComBoBox();
	ui->comboBox->setCurrentText(m_strName);
}

MainWindow::~MainWindow()
{
	delete m_dPlc;
	delete m_dCamera;
	delete m_dDetection;
	delete ui;
}

void MainWindow::systimer() {
	if (m_bStart) {
		// 计算速率
		m_nTime++;
		if (m_nTime - m_nOldTime == 5) {
			m_nOldTime = m_nTime;
			strRev = QString("%1%2").arg(QString::number((((float)m_nCameraResult - (float)m_nResult) / 5) * 60))
				.arg(" pcs/min");
			m_nResult = m_nCameraResult;
		}
		// 记录运行时长
		SetRunTime();
	}
	// 判断PLC连接
	IsPlcConnect();

	// 设置时钟
	SetClock();

}

void MainWindow::IsPlcConnect() {
	QString msg;
	if (m_tPlc->m_worker->m_bPlcConnect) {
		msg = QString::fromLocal8Bit("成功：PLC连接成功");
	}
	else {
		msg = QString::fromLocal8Bit("错误：PLC连接失败");
	}
	ui->msgPlc->setText(msg);
}
void MainWindow::SetClock() {
	ui->label_time->setText(GetTime());
}

void MainWindow::SetRunTime() {
	m_sRunTime = QString("%1h:%2m:%3s").arg(m_nHH).arg(m_nMM, 2, 10, QLatin1Char('0')).arg(m_nSS, 2, 10, QLatin1Char('0'));
	ui->label_27->setText(m_sRunTime);
	m_nSS++;
	if (m_nSS > 59) {
		m_nSS = 0;
		m_nMM++;
	}
	if (m_nMM > 59) {
		m_nMM = 0;
		m_nHH++;
	}
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
	if (obj == ui->camera_1)//指定某个QLabel
	{
		if (event->type() == QEvent::MouseButtonDblClick)//mouse button pressed
		{
			ui->bigLbale->show();
			m_tCamera1->m_worker->m_cData.hv_WindowHandle = m_hvBigMainHandle;
			return false;
		}
		return false;
	}
	if (obj == ui->camera_2)//指定某个QLabel
	{
		if (event->type() == QEvent::MouseButtonDblClick)//mouse button pressed
		{
			ui->bigLbale->show();

			m_tCamera2->m_worker->m_cData.hv_WindowHandle = m_hvBigMainHandle;
			return false;
		}
		return false;
	}
	if (obj == ui->camera_3)//指定某个QLabel
	{
		if (event->type() == QEvent::MouseButtonDblClick)//mouse button pressed
		{
			if (ui->bigLbale->isHidden()) {
				ui->bigLbale->show();
				m_tCamera3->m_worker->m_cData.hv_WindowHandle = m_hvBigMainHandle;
			}
			return false;
		}
		return false;
	}
	if (obj == ui->camera_4)//指定某个QLabel
	{
		if (event->type() == QEvent::MouseButtonDblClick)//mouse button pressed
		{
			ui->bigLbale->show();

			m_tCamera4->m_worker->m_cData.hv_WindowHandle = m_hvBigMainHandle;
			return false;
		}
		return false;
	}
	if (obj == ui->bigLbale)//指定某个QLabel
	{
		if (event->type() == QEvent::MouseButtonDblClick)//mouse button pressed
		{
			ui->bigLbale->hide();
			SetHalconWindows();
			return false;
		}
		return false;
	}
	else
	{
		// pass the event on to the parent class
		return MainWindow::eventFilter(obj, event);
	}
}



// 界面设置
void MainWindow::setInterface() {

	// 权限隐藏
	ui->rightButton_1_1->hide();
	ui->rightButton_1_2->hide();
	ui->rightButton_1_3->hide();
	ui->rightButton_1_4->hide();
	ui->rightButton_1_5->hide();

	ui->bigLbale->hide();
	// 去掉边框
	setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
	m_dPlc = new PlcDl(this);
	m_dPlc->setVisible(false);
	m_dCamera = new CameraDlg(this);
	m_dCamera->setVisible(false);
	m_dDetection = new DetectionDlg(this);
	m_dDetection->setVisible(false);
	m_dLogin = new LoginDLg(this);
	m_dLogin->show();

	m_dProduct = new ProductDlg(this);
	m_dProduct->hide();

	// 按钮
	ui->startButton->hide();
	ui->stopButton->hide();
	// dlg 切换
	connect(m_dPlc, &PlcDl::changeDlg, this, &MainWindow::DlgListChanged);
	connect(m_dCamera, &CameraDlg::changeDlg, this, &MainWindow::DlgListChanged);
	connect(m_dDetection, &DetectionDlg::changeDlg, this, &MainWindow::DlgListChanged);
}
// 数据初始化
void MainWindow::Initialize() {
	CreateCameraSaveFile();
	//注册数据类型 不然信号槽会数据丢失
	qRegisterMetaType<Ui::MainWindow*>("Ui::MainWindow*");
	qRegisterMetaType<FramePtr>("FramePtr");
	qRegisterMetaType<HalconCpp::HObject>("HalconCpp::HObject");
	qRegisterMetaType<CameraData>("CameraData");
	qRegisterMetaType<DetectionDlgInfo>("DetectionDlgInfo");
	qRegisterMetaType<CameraDlgInfo>("CameraDlgInfo");
	qRegisterMetaType<SIG_PLC>("SIG_PLC");
	qRegisterMetaType<CsvData>("CsvData");

	// Camera Thread

	m_tSaveImg1 = new SaveImgThread();
	m_tSaveImg2 = new SaveImgThread();
	m_tSaveImg3 = new SaveImgThread();
	m_tSaveImg4 = new SaveImgThread();

	// Camera Thread
	m_tCamera1 = new CameraThread();
	m_tCamera2 = new CameraThread();
	m_tCamera3 = new CameraThread();
	m_tCamera4 = new CameraThread();


	// Alg Thread
	m_tAlg1 = new AlgThread();
	m_tAlg2 = new AlgThread();
	m_tAlg3 = new AlgThread();
	m_tAlg4 = new AlgThread();


	//  Plc Thread
	m_tPlc = new PlcThread();
	m_tPlc->Initialize();
	// Csv Thread
	m_tCsv = new CsvThread();
	//    m_csData.csUser = "admin";(舍弃)
	// Star Vimba
	VmbErrorType err = m_ApiController.StartUp();
	Log("Starting Vimba", err);


	// Connect Camera and Alg Thread event
	connect(m_tCamera1->m_worker, &CameraWorker::sigAlgStart, m_tAlg1->m_worker, &AlgWorker::start);
	connect(m_tCamera2->m_worker, &CameraWorker::sigAlgStart, m_tAlg2->m_worker, &AlgWorker::start);
	connect(m_tCamera3->m_worker, &CameraWorker::sigAlgStart, m_tAlg3->m_worker, &AlgWorker::start);
	connect(m_tCamera4->m_worker, &CameraWorker::sigAlgStart, m_tAlg4->m_worker, &AlgWorker::start);



	connect(m_tAlg1->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg1->m_worker, &SaveImgWorker::start);
	connect(m_tAlg2->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg2->m_worker, &SaveImgWorker::start);
	connect(m_tAlg3->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg3->m_worker, &SaveImgWorker::start);
	connect(m_tAlg4->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg4->m_worker, &SaveImgWorker::start);


	connect(m_tAlg4->m_worker, &AlgWorker::sigCameraResult, this, &MainWindow::CameraResult);


	// Connect Camera and Alg Thread event
	connect(m_dCamera, &CameraDlg::sigCameraDlgInfo, this, &MainWindow::ReceiveCameraDlgInfo);
	//PLC
	connect(m_dPlc, &PlcDl::sigWdPlc, this, &MainWindow::WdPlcInfo);
	connect(m_dPlc, &PlcDl::sigRdPlc, this, &MainWindow::RdPlcInfo);

	connect(m_dDetection, &DetectionDlg::sigDltoDetection, this, &MainWindow::ReceiveDetecDlgInfo);
	connect(m_dLogin, &LoginDLg::sigLogDlgInfo, this, &MainWindow::ReceiveLoginDlgInfo);
	connect(m_dProduct->m_dProCreat, &ProCreateDlg::sigProCreateToPro, this, &MainWindow::ReceiveProDlgInfo);

	connect(m_dProduct, &ProductDlg::sigProToMain, this, &MainWindow::ReceiveProDlgInfo);



	if (VmbErrorSuccess == err)
	{
		// Connect new camera found event with event handler
		QObject::connect(m_ApiController.GetCameraObserver(), SIGNAL(CameraListChangedSignal(int)), this, SLOT(OnCameraListChanged(int)));
		// Initially get all connected cameras
		UpdateCameraListBox();

	}
}

void MainWindow::on_startButton_clicked()
{
	OpenHalconWindows();
	SetHalconWindows();
	SetAlgModel();
	// start
	if (m_bIsCamera) {

		if (ui->stopButton->isHidden()) {
			ui->startButton->hide();
			ui->stopButton->show();
		}

		Log("start camera");
		m_bStart = true;
		ui->clearBt->setEnabled(false);
		ui->rightButton_1_5->setEnabled(false);
		m_nIdex = 0; // PLC 通信计数
		// 启动 Plc
		m_tPlc->m_worker->writePlcInfo(START, 1);
		m_tPlc->m_worker->writePlcInfo(STOP, 0);

		// csv数据（舍弃）
		//        m_csData.csStarTime = GetTime();
	}
	else {
		Log("Plc Connect erro");
	}
	ui->checkBox->setEnabled(false);
	ui->comboBox->setEnabled(false);
}

void MainWindow::on_stopButton_clicked()
{
	if (m_bIsCamera) {
		// stop
		if (ui->startButton->isHidden()) {
			ui->stopButton->hide();
			ui->startButton->show();
		}
		Log("Stopping Acquisition");

		m_bStart = false;
		ui->clearBt->setEnabled(true);
		// 停止 Plc
		m_tPlc->m_worker->writePlcInfo(START, 0);
		m_tPlc->m_worker->writePlcInfo(STOP, 1);
		// csv数据 写入（舍弃）
		//        m_csData.csStopTime = GetTime();
		//        m_tCsv->WriteIn(m_csData);
	}
	else {
		Log("Plc Connect erro");
	}
	ui->checkBox->setEnabled(true);
	ui->comboBox->setEnabled(true);
	ui->rightButton_1_5->setEnabled(true);
	SaveToIni();
}
void MainWindow::on_minButton_clicked()
{
	// min
	setWindowState(Qt::WindowMinimized);
}

void MainWindow::on_quiteButton_clicked()
{
	SaveToIni();
	// quit
	if (!(QMessageBox::question(this, tr("Exit"), QString::fromLocal8Bit("退出？"), tr("Yes"), tr("No"))))
	{
		QApplication* app;
		app->exit(0);
	}

}



void MainWindow::Log(std::string strMsg, VmbErrorType eErr)
{
	strMsg += "..." + m_ApiController.ErrorCodeToMessage(eErr);
	//    ui->msgList->insertItem(0, QString::fromStdString(strMsg));

}

void MainWindow::Log(std::string strMsg)
{
	//    ui->msgList->insertItem(0, QString::fromStdString(strMsg));
}
void MainWindow::UpdateCameraListBox()
{
	// Get all cameras currently connected to Vimba
	CameraPtrVector cameras = m_ApiController.GetCameraList();

	std::stringstream strMsg;
	strMsg << "Cameras found..." << cameras.size();
	Log(strMsg.str());
	if (cameras.size() == 4) {
		m_tCamera1->m_worker->m_cData.m_nIndex = 1;
		m_tCamera2->m_worker->m_cData.m_nIndex = 2;
		m_tCamera3->m_worker->m_cData.m_nIndex = 3;
		m_tCamera4->m_worker->m_cData.m_nIndex = 4;


		m_tCamera1->m_worker->m_cData.rStrCameraID = m_cameras[0];
		m_tCamera2->m_worker->m_cData.rStrCameraID = m_cameras[1];
		m_tCamera3->m_worker->m_cData.rStrCameraID = m_cameras[2];
		m_tCamera4->m_worker->m_cData.rStrCameraID = m_cameras[3];
		m_tCamera1->Initialize();
		m_tCamera2->Initialize();
		m_tCamera3->Initialize();
		m_tCamera4->Initialize();


		ui->startButton->show();

		m_tCamera1->start();
		m_tCamera2->start();
		m_tCamera3->start();
		m_tCamera4->start();
	}
}

void MainWindow::OnCameraListChanged(int reason)
{
	bool bUpdateList = false;

	// We only react on new cameras being found and known cameras being unplugged
	if (AVT::VmbAPI::UpdateTriggerPluggedIn == reason)
	{
		Log("Camera list changed. A new camera was discovered by Vimba.");
		bUpdateList = true;
		if (m_cameras.size() == 4) {
			m_bIsCamera = true;
		}
	}
	else if (AVT::VmbAPI::UpdateTriggerPluggedOut == reason)
	{
		Log("Camera list changed. A camera was disconnected from Vimba.");

		m_bIsCamera = false;
		bUpdateList = true;
	}
}
void MainWindow::DlgListChanged(int Index) {
	if (!m_bAdmin) {
		return;
	}
	switch (Index)
	{
	case 1:
		m_dDetection->hide();
		m_dCamera->hide();
		m_dPlc->hide();
		RdProductIni(file_list[m_nProductNum]);
		break;
	case 2:
		m_dDetection->show();
		m_dCamera->hide();
		m_dPlc->hide();
		break;
	case 3:
		m_dDetection->hide();
		m_dCamera->show();
		m_dPlc->hide();
		break;
	case 4:
		m_dDetection->hide();
		m_dCamera->hide();
		m_dPlc->show();
		break;
	case 5:
		m_dDetection->hide();
		m_dCamera->hide();
		m_dPlc->show();
		break;
	default:
		m_dDetection->hide();
		m_dCamera->hide();
		m_dPlc->hide();
		break;
	}
}


void MainWindow::on_switchBt_3_clicked()
{
	DlgListChanged(3);
}

void MainWindow::on_switchBt_4_clicked()
{
	DlgListChanged(4);
}

void MainWindow::on_switchBt_2_clicked()
{
	DlgListChanged(2);
}


void MainWindow::OpenHalconWindows()
{
	//// Halcon
	if (m_nOpenHalcon) {
		return;
	}
	m_nOpenHalcon = 1;
	labe_1 = ui->camera_1->winId();
	labe_2 = ui->camera_2->winId();
	labe_3 = ui->camera_3->winId();
	labe_4 = ui->camera_4->winId();

	big_labe = ui->bigLbale->winId();

	HalconCpp::OpenWindow(0, 0, ui->camera_1->width(), ui->camera_1->height(), labe_1, "", "", &m_hvMainHandle1);
	HalconCpp::OpenWindow(0, 0, ui->camera_2->width(), ui->camera_2->height(), labe_2, "", "", &m_hvMainHandle2);
	HalconCpp::OpenWindow(0, 0, ui->camera_3->width(), ui->camera_3->height(), labe_3, "", "", &m_hvMainHandle3);
	HalconCpp::OpenWindow(0, 0, ui->camera_4->width(), ui->camera_4->height(), labe_4, "", "", &m_hvMainHandle4);


	HalconCpp::OpenWindow(0, 0, ui->bigLbale->width(), ui->bigLbale->height(), big_labe, "", "", &m_hvBigMainHandle);
}



void MainWindow::CloseHalconWindows()
{
	HalconCpp::CloseWindow(m_hvMainHandle1);
	HalconCpp::CloseWindow(m_hvMainHandle2);
	HalconCpp::CloseWindow(m_hvMainHandle3);
	HalconCpp::CloseWindow(m_hvMainHandle4);
}

void MainWindow::CameraResult(int m_nResult)
{
	// 发送给PLC 结果
	AlgRule(m_nResult);
	// 界面显示
	ShowUI();
}


void MainWindow::AlgRule(int nIndex) {

	int m_nNGorOK = 0;
	int nNGorOK1 = m_tAlg1->m_worker->m_nNGorOK[nIndex];
	int nNGorOK2 = m_tAlg2->m_worker->m_nNGorOK[nIndex];
	int nNGorOK3 = m_tAlg3->m_worker->m_nNGorOK[nIndex];
	int nNGorOK4 = m_tAlg4->m_worker->m_nNGorOK[nIndex];

	qDebug() << "AlgRule:" << nNGorOK1 << nNGorOK2 << nNGorOK3 << nNGorOK4;
	if (m_nAlgSw1 == 0) {
		nNGorOK1 = 0;
	}
	else {
		if (nNGorOK1) {
			m_nCameraInfo1.badSum++;
		}
		else {
			m_nCameraInfo1.goodSum++;
		}
		m_nCameraInfo1.checkSum++;
	}
	if (m_nAlgSw2 == 0) {
		nNGorOK2 = 0;
	}
	else {
		if (nNGorOK2) {
			m_nCameraInfo2.badSum++;
		}
		else {
			m_nCameraInfo2.goodSum++;
		}
		m_nCameraInfo2.checkSum++;
	}
	if (m_nAlgSw3 == 0) {
		nNGorOK3 = 0;
	}
	else {
		if (nNGorOK3) {
			m_nCameraInfo3.badSum++;
		}
		else {
			m_nCameraInfo3.goodSum++;
		}
		m_nCameraInfo3.checkSum++;
	}
	if (m_nAlgSw4 == 0) {
		nNGorOK4 = 0;
	}
	else {
		if (nNGorOK4) {
			m_nCameraInfo4.badSum++;
		}
		else {
			m_nCameraInfo4.goodSum++;
		}
		m_nCameraInfo4.checkSum++;
	}


	// csv 数据存入
	if (ui->checkBox->isChecked() == true) {
		m_csData.csStarTime = GetTime();
		m_csData.csIndex = QString::number(m_nCameraResult);
		m_csData.csIn_Round = QString::number(m_tAlg1->m_worker->fIn_Round[nIndex]);
		m_csData.csOut_Round = QString::number(m_tAlg1->m_worker->fOut_Round[nIndex]);
		m_csData.csIn_Len = QString::number(m_tAlg1->m_worker->fIn_Len[nIndex]);
		m_csData.csOut_Len = QString::number(m_tAlg1->m_worker->fOut_Len[nIndex]);
		m_csData.csLen = QString::number(m_tAlg2->m_worker->fLen[nIndex]);
		m_csData.csNGorOK = QString::number(m_nNGorOK);
		m_tCsv->WriteIn(m_csData);
	}

	// 删除图片

	if (m_tSaveImg1->m_worker->m_nIndex > 300) {
		QString imgPath = QDir::currentPath() + "/Pic/ccd1";
		m_dDetection->deleteDir(imgPath);
		m_tSaveImg1->m_worker->m_nIndex = 0;
	}

	if (m_tSaveImg2->m_worker->m_nIndex > 300) {
		QString imgPath = QDir::currentPath() + "/Pic/ccd2";
		m_dDetection->deleteDir(imgPath);
		m_tSaveImg2->m_worker->m_nIndex = 0;
	}
	if (m_tSaveImg3->m_worker->m_nIndex > 300) {
		QString imgPath = QDir::currentPath() + "/Pic/ccd3";
		m_dDetection->deleteDir(imgPath);
		m_tSaveImg3->m_worker->m_nIndex = 0;
	}
	if (m_tSaveImg4->m_worker->m_nIndex > 300) {
		QString imgPath = QDir::currentPath() + "/Pic/ccd4";
		m_dDetection->deleteDir(imgPath);
		m_tSaveImg4->m_worker->m_nIndex = 0;
	}




	m_nCameraResult++;  //当前计数



	if (nNGorOK3 == 0 && nNGorOK4 == 0 && nNGorOK1 == 0 && nNGorOK2 == 0)
	{
		m_nNGorOK = 102;
		m_nOK++;
		SendPlcResult(m_nNGorOK);
		return;
	}
	else
	{
		if (nNGorOK3 > 1 || nNGorOK4 > 1 || nNGorOK1 > 1 || nNGorOK2 > 1) {
            if (nNGorOK1 == 1)
			{
				//NG2 尺寸
				m_nNGorOK = 101;
				m_nNG2++;
				SendPlcResult(m_nNGorOK);
				return;
            }
			//未检测
			m_nNGorOK = 200;
			m_nNG3++;
			SendPlcResult(m_nNGorOK);
			return;
		}

		if (nNGorOK1 == 1 || nNGorOK2 == 1)
		{
			//NG2 尺寸
			m_nNGorOK = 101;
			m_nNG2++;
			SendPlcResult(m_nNGorOK);
			return;
		}

		if (nNGorOK3 == 1 || nNGorOK4 == 1) {
			//NG1  外观
			m_nNGorOK = 100;
			m_nNG1++;
			SendPlcResult(m_nNGorOK);
			return;
		}
	}
}

void MainWindow::SendPlcResult(int result)
{
	m_tPlc->m_worker->writePlcInfo(RESULT_NUMBER, m_nIdex);
	m_tPlc->m_worker->writePlcInfo(CAMERA_RESULT, result);
	qDebug() << "SendPlcResult:" << m_nIdex << result;
	m_nIdex += 2;
	if (m_nIdex == 200) {
		m_nIdex = 0;
	}
}


void MainWindow::on_rightButton_1_1_clicked()
{

	// 玻璃盘start stop
	if (m_nBtrightButton_1_1)
	{
		ui->rightButton_1_1->setStyleSheet("background-image: url(:/png/imge/main/right_1_1A.png);");
	}
	else {
		ui->rightButton_1_1->setStyleSheet("background-image: url(:/png/imge/main/right_1_1B.png);");
	}
	m_nBtrightButton_1_1 = 1 - m_nBtrightButton_1_1;
	m_tPlc->m_worker->writePlcInfo(MOTOR1, m_nBtrightButton_1_1);
}

void MainWindow::on_rightButton_1_2_clicked()
{
	// 震动盘 start stop
	if (m_nBtrightButton_1_2)
	{
		ui->rightButton_1_2->setStyleSheet("background-image: url(:/png/imge/main/right_1_2A.png);");
	}
	else {
		ui->rightButton_1_2->setStyleSheet("background-image: url(:/png/imge/main/right_1_2B.png);");
	}
	m_nBtrightButton_1_2 = 1 - m_nBtrightButton_1_2;
	m_tPlc->m_worker->writePlcInfo(QPLATE, m_nBtrightButton_1_2);
}

void MainWindow::on_rightButton_1_4_clicked()
{
	// 启动照明
	if (m_nBtrightButton_1_4)
	{
		ui->rightButton_1_4->setStyleSheet("background-image: url(:/png/imge/main/right_1_4A.png);");
	}
	else {
		ui->rightButton_1_4->setStyleSheet("background-image: url(:/png/imge/main/right_1_4B.png);");
	}
	m_nBtrightButton_1_4 = 1 - m_nBtrightButton_1_4;
	m_tPlc->m_worker->writePlcInfo(QLINE, m_nBtrightButton_1_4);
}

void MainWindow::on_rightButton_1_3_clicked()
{
	// 启动上料电机
	if (m_nBtrightButton_1_3)
	{
		ui->rightButton_1_3->setStyleSheet("background-image: url(:/png/imge/main/right_1_3A.png);");
	}
	else {
		ui->rightButton_1_3->setStyleSheet("background-image: url(:/png/imge/main/right_1_3B.png);");
	}
	m_nBtrightButton_1_3 = 1 - m_nBtrightButton_1_3;
	m_tPlc->m_worker->writePlcInfo(MOTOR2, m_nBtrightButton_1_3);
}

void MainWindow::ReceiveCameraDlgInfo(CameraDlgInfo cameraInfo) {
	if (m_bIsCamera) {
		switch (cameraInfo.m_nFunction) {
			// 0.无 1.打开相机获取相机值 2.启动硬触发 3.启动连续采集 4.保存相机数据
		case 0:
			break;
		case 1:
			RdDlgCameraInfo(cameraInfo);
			break;
		case 2:
			HardToTrigger(cameraInfo);
			break;
		case 3:
			StartDlgConsecutive(cameraInfo);
			break;
		case 4:
			WdDlgCameraInfo(cameraInfo);
			break;
		default:
			break;
		}
	}
}
void MainWindow::RdDlgCameraInfo(CameraDlgInfo cameraInfo) {
	double ExposureTimeAbs;
	VmbInt64_t OffsetX;
	VmbInt64_t OffsetY;
	VmbInt64_t Height;
	VmbInt64_t Width;
	CameraThread* tCamera;
	switch (cameraInfo.m_nCamera) {
	case 1:
		tCamera = m_tCamera1;
		break;
	case 2:
		tCamera = m_tCamera2;
		break;
	case 3:
		tCamera = m_tCamera3;
		break;
	case 4:
		tCamera = m_tCamera4;
		break;
	default:
		break;
	}
	SetHalconWindows();
	tCamera->m_worker->m_cData.hv_WindowHandle = cameraInfo.hv_WindowHandle;
	tCamera->m_worker->m_ApiController->GetFeatureDoubleValue("ExposureTimeAbs", ExposureTimeAbs);
	tCamera->m_worker->m_ApiController->GetFeatureIntValue("OffsetX", OffsetX);
	tCamera->m_worker->m_ApiController->GetFeatureIntValue("OffsetY", OffsetY);
	tCamera->m_worker->m_ApiController->GetFeatureIntValue("Height", Height);
	tCamera->m_worker->m_ApiController->GetFeatureIntValue("Width", Width);


	m_dCamera->ui->lineEdit_1->setText(QString::number(ExposureTimeAbs));
	m_dCamera->ui->lineEdit_2->setText(QString::number(OffsetX));
	m_dCamera->ui->lineEdit_3->setText(QString::number(OffsetY));
	m_dCamera->ui->lineEdit_4->setText(QString::number(Height));
	m_dCamera->ui->lineEdit_5->setText(QString::number(Width));

	m_dCamera->ui->lineEdit_6->setText(QString::number(ExposureTimeAbs));
	m_dCamera->ui->lineEdit_7->setText(QString::number(OffsetX));
	m_dCamera->ui->lineEdit_8->setText(QString::number(OffsetY));
	m_dCamera->ui->lineEdit_9->setText(QString::number(Height));
	m_dCamera->ui->lineEdit_10->setText(QString::number(Width));
}
void MainWindow::StartDlgConsecutive(CameraDlgInfo cameraInfo) {
	AlgThread* m_tAlg;
	CameraThread* m_tCamera;
	switch (cameraInfo.m_nCamera) {
	case 1:
		m_tAlg = m_tAlg1;
		m_tCamera = m_tCamera1;
		break;
	case 2:
		m_tAlg = m_tAlg2;
		m_tCamera = m_tCamera2;
		break;
	case 3:
		m_tAlg = m_tAlg3;
		m_tCamera = m_tCamera3;
		break;
	case 4:
		m_tAlg = m_tAlg4;
		m_tCamera = m_tCamera4;
		break;
	default:
		break;
	}
	if (cameraInfo.m_nConsecutive) {
		m_tAlg->m_worker->m_nModel = 1; // 手动模式

		// 不存图
		m_tAlg->m_worker->m_nSaveImage = 1;
		m_tCamera->m_worker->m_ApiController->SetInternalMode(2);

	}
	else {
		// 可以存图
		m_tAlg->m_worker->m_nSaveImage = 0;
		m_tCamera->m_worker->m_ApiController->SetInternalMode(0);
	}
}
void MainWindow::WdDlgCameraInfo(CameraDlgInfo cameraInfo) {
	CameraThread* tCamera;
	switch (cameraInfo.m_nCamera) {
	case 1:
		tCamera = m_tCamera1;
		break;
	case 2:
		tCamera = m_tCamera2;
		break;
	case 3:
		tCamera = m_tCamera3;
		break;
	case 4:
		tCamera = m_tCamera4;
		break;
	default:
		break;
	}
	tCamera->m_worker->m_ApiController->SetFeatureDoubleValue("ExposureTimeAbs", cameraInfo.m_nParameter.ExposureTimeAbs);
	tCamera->m_worker->m_ApiController->SetFeatureIntValue("OffsetX", cameraInfo.m_nParameter.OffsetX);
	tCamera->m_worker->m_ApiController->SetFeatureIntValue("OffsetY", cameraInfo.m_nParameter.OffsetY);
	tCamera->m_worker->m_ApiController->SetFeatureIntValue("Height", cameraInfo.m_nParameter.Height);
	tCamera->m_worker->m_ApiController->SetFeatureIntValue("Width", cameraInfo.m_nParameter.Width);

	RdDlgCameraInfo(cameraInfo);
}

void MainWindow::HardToTrigger(CameraDlgInfo cameraInfo) {
	switch (cameraInfo.m_nCamera) {
	case 1:
		m_tAlg1->m_worker->m_nModel = 1;
		if (cameraInfo.m_nSingle) {

			m_tPlc->m_worker->writePlcInfo(QGRAP1, cameraInfo.m_nSingle);
		}
		else {
			m_tPlc->m_worker->writePlcInfo(QGRAP1, cameraInfo.m_nSingle);
		}
		break;
	case 2:
		m_tAlg2->m_worker->m_nModel = 1;
		if (cameraInfo.m_nSingle) {
			m_tPlc->m_worker->writePlcInfo(QGRAP2, cameraInfo.m_nSingle);
		}
		else {
			m_tPlc->m_worker->writePlcInfo(QGRAP2, cameraInfo.m_nSingle);
		}
		break;
	case 3:
		m_tAlg3->m_worker->m_nModel = 1;
		if (cameraInfo.m_nSingle) {
			m_tPlc->m_worker->writePlcInfo(QGRAP3, cameraInfo.m_nSingle);
		}
		else {
			m_tPlc->m_worker->writePlcInfo(QGRAP3, cameraInfo.m_nSingle);
		}
		break;
	case 4:
		m_tAlg4->m_worker->m_nModel = 1;
		if (cameraInfo.m_nSingle) {
			m_tPlc->m_worker->writePlcInfo(QGRAP4, cameraInfo.m_nSingle);
		}
		else {
			m_tPlc->m_worker->writePlcInfo(QGRAP4, cameraInfo.m_nSingle);
		}
		break;
	}
}

void MainWindow::RdConfigIni()
{
	QString iniFilePath = "Config/config.ini";
	QSettings configIniRead(iniFilePath, QSettings::IniFormat);
	configIniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

	// 获取相机
	int nCameraNum = configIniRead.value("CameraNum/Num").toInt();
	for (int i = 0; i < nCameraNum; i++) {
		QString camera_fileName =
			QString("%1%2%3")
			.arg("Camera_").arg(i + 1).arg("/rStrCameraID");
		std::string camera = configIniRead.value(camera_fileName).toString().toStdString();
		m_cameras.push_back(camera);
	}

	RdManufactureIni();
}

void MainWindow::RdProductIni(QString iniFilePath)
{
	QString Path = "Config/Product/" + iniFilePath;
	QSettings iniRead(Path, QSettings::IniFormat);
	iniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

	m_dDetection->m_dRoi->m_sIniFilePath = iniFilePath;
	// 获取检测参数
	QString sInfo;
	float* fIndex1 = &m_detection.m_Cam1.Scale;
	float* fIndex2 = &m_detection.m_Cam2.Scale;
	int* nIndex1 = &m_detection.m_Cam3.parame0;
	int* nIndex2 = &m_detection.m_Cam4.parame0;
	for (int x = 0; x < 4; x++) {
		switch (x) {
		case 0:
			for (int a = 0; a < 17; a++)
			{
				sInfo = QString("Detection_1/parame%1").arg(a);
				*fIndex1 = iniRead.value(sInfo).toFloat();
				fIndex1++;
			}
			break;
		case 1:
			for (int a = 0; a < 9; a++)
			{
				sInfo = QString("Detection_2/parame%1").arg(a);
				*fIndex2 = iniRead.value(sInfo).toFloat();
				fIndex2++;
			}
			break;
		case 2:
			for (int a = 0; a < 25; a++) {
				sInfo = QString("Detection_3/parame%1").arg(a);
				*nIndex1 = iniRead.value(sInfo).toInt();
				nIndex1++;
			}
			break;
		case 3:
			for (int a = 0; a < 25; a++) {
				sInfo = QString("Detection_4/parame%1").arg(a);
				*nIndex2 = iniRead.value(sInfo).toInt();
				nIndex2++;
			}
			break;
		}
	}
	SetCameraParameter();
	if (m_dDetection->m_detectionInfo.m_nCamera) {
		m_dDetection->CameraList(m_dDetection->m_detectionInfo.m_nCamera);
		m_dDetection->ui->tabWidget->setCurrentIndex(m_dDetection->m_detectionInfo.m_nCamera - 1);
	}

	HalconCpp::HTuple* fAlgPlaceA1 = &m_tAlg1->m_worker->m_algPlaceA.x1;
	HalconCpp::HTuple* fAlgPlaceA2 = &m_tAlg2->m_worker->m_algPlaceA.x1;
	HalconCpp::HTuple* fAlgPlaceA3 = &m_tAlg3->m_worker->m_algPlaceA.x1;
	HalconCpp::HTuple* fAlgPlaceA4 = &m_tAlg4->m_worker->m_algPlaceA.x1;


	QList<HalconCpp::HTuple*> m_lAlgPlaceA = QList<HalconCpp::HTuple*>() << fAlgPlaceA1 << fAlgPlaceA2 << fAlgPlaceA3 << fAlgPlaceA4;


	HalconCpp::HTuple* fAlgPlaceB1 = &m_tAlg1->m_worker->m_algPlaceB.x1;
	HalconCpp::HTuple* fAlgPlaceB2 = &m_tAlg2->m_worker->m_algPlaceB.x1;
	HalconCpp::HTuple* fAlgPlaceB3 = &m_tAlg3->m_worker->m_algPlaceB.x1;
	HalconCpp::HTuple* fAlgPlaceB4 = &m_tAlg4->m_worker->m_algPlaceB.x1;


	QList<HalconCpp::HTuple*> m_lAlgPlaceB = QList<HalconCpp::HTuple*>() << fAlgPlaceB1 << fAlgPlaceB2 << fAlgPlaceB3 << fAlgPlaceB4;

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {

			*m_lAlgPlaceA[i] = iniRead.value(QString("AlgPlaceA_%1/parame%2").arg(i + 1).arg(j)).toFloat();
			m_lAlgPlaceA[i]++;

			*m_lAlgPlaceB[i] = iniRead.value(QString("AlgPlaceB_%1/parame%2").arg(i + 1).arg(j)).toFloat();
			m_lAlgPlaceB[i]++;
		}


	}
}

void MainWindow::DirFile(QString iniFilePath)
{
	QFile fileTemp(iniFilePath);
	fileTemp.remove();
}

void MainWindow::SearchProFiles()
{

	// 获取当前文件所有图片名称
	QString path = QDir::currentPath() + "/Config/Product";
	QDir dir(path);
	file_list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
	if (file_list.size() == 1) {
		m_dProduct->ui->deleteBt->setEnabled(false);
	}
	else {
		m_dProduct->ui->deleteBt->setEnabled(true);
	}
}


void MainWindow::ReceiveDetecDlgInfo(DetectionDlgInfo detectionInfo) {
	// 0.无 1.设置检测参数 2.修改检测参数 3. 4
	switch (detectionInfo.m_nFunction) {
	case 1:
		SetDetectionDlgUI(detectionInfo);
		break;
	case 2:
		WriteIni(detectionInfo);
		break;
	default:
		break;
	}

}
void MainWindow::SetDetectionDlgUI(DetectionDlgInfo detectionInfo) {
	QString lineText;
	float* fIndex1 = &m_detection.m_Cam1.Scale;
	float* fIndex2 = &m_detection.m_Cam2.Scale;
	int* nIndex1 = &m_detection.m_Cam3.parame0;
	int* nIndex2 = &m_detection.m_Cam4.parame0;
	QLineEdit* lE;
	switch (detectionInfo.m_nCamera) {
	case 1:
		for (int i = 0; i < 17; i++) {
			lineText = QString::number(*fIndex1);
			lE = m_dDetection->findChild<QLineEdit*>("cm1_" + QString::number(i + 1));
			lE->setText(lineText);
			fIndex1++;
		}
		break;
	case 2:
		for (int i = 0; i < 9; i++) {
			lineText = QString::number(*fIndex2);
			lE = m_dDetection->findChild<QLineEdit*>("cm2_" + QString::number(i + 1));
			lE->setText(lineText);
			fIndex2++;
		}
		break;
	case 3:
		for (int i = 0; i < 25; i++) {
			lineText = QString::number(*nIndex1);
			lE = m_dDetection->findChild<QLineEdit*>("cm3_" + QString::number(i + 1));
			lE->setText(lineText);
			nIndex1++;
		}
		break;
	case 4:
		for (int i = 0; i < 25; i++) {
			lineText = QString::number(*nIndex2);
			lE = m_dDetection->findChild<QLineEdit*>("cm4_" + QString::number(i + 1));
			lE->setText(lineText);
			nIndex2++;
		}
		break;
	default:
		break;
	}
}


void MainWindow::WriteIni(DetectionDlgInfo detectionInfo) {
	float* fIndex1 = &m_detection.m_Cam1.Scale;
	float* fIndex2 = &m_detection.m_Cam2.Scale;
	int* nIndex1 = &m_detection.m_Cam3.parame0;
	int* nIndex2 = &m_detection.m_Cam4.parame0;

	QLineEdit* lE;
	QString iniFilePath = "Config/Product/" + file_list[m_nProductNum];
	QSettings configIniWrite(iniFilePath, QSettings::IniFormat);
	configIniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
	QString sInfo;
	switch (detectionInfo.m_nCamera) {
	case 1:
		for (int i = 0; i < 17; i++) {
			lE = m_dDetection->findChild<QLineEdit*>("cm1_" + QString::number(i + 1));
			*fIndex1 = lE->text().toFloat();
			sInfo = QString("Detection_1/parame%1").arg(i);
			configIniWrite.setValue(sInfo, lE->text());
			fIndex1++;
		}
		break;
	case 2:
		for (int i = 0; i < 9; i++) {
			lE = m_dDetection->findChild<QLineEdit*>("cm2_" + QString::number(i + 1));
			*fIndex2 = lE->text().toFloat();
			sInfo = QString("Detection_2/parame%1").arg(i);
			configIniWrite.setValue(sInfo, lE->text());

			fIndex2++;
		}
		break;
	case 3:
		for (int i = 0; i < 25; i++) {
			lE = m_dDetection->findChild<QLineEdit*>("cm3_" + QString::number(i + 1));
			*nIndex1 = lE->text().toInt();
			sInfo = QString("Detection_3/parame%1").arg(i);
			configIniWrite.setValue(sInfo, lE->text());
			nIndex1++;
		}
		break;
	case 4:
		for (int i = 0; i < 25; i++) {
			lE = m_dDetection->findChild<QLineEdit*>("cm4_" + QString::number(i + 1));
			*nIndex2 = lE->text().toInt();
			sInfo = QString("Detection_4/parame%1").arg(i);
			configIniWrite.setValue(sInfo, lE->text());
			nIndex2++;
		}
		break;
	default:
		break;
	}
	QMessageBox::information(this, tr("Success"), QString::fromLocal8Bit("写入成功！"));
	SetCameraParameter();
	SetDetectionDlgUI(detectionInfo);
}

void MainWindow::SetComBoBox()
{
	ui->comboBox->clear();
	file_names.clear();
	// comboBox
	for (int i = 0; i < file_list.size(); i++) {
		file_names.append(file_list[i].left(file_list[i].indexOf(".ini")));
		ui->comboBox->addItem(file_names[i]);
	}
}

void MainWindow::ReceiveLoginDlgInfo(int nIndex) {
	switch (nIndex) {
	case 1:
		m_bAdmin = true;
		ui->rightButton_1_1->show();
		ui->rightButton_1_2->show();
		ui->rightButton_1_3->show();
		ui->rightButton_1_4->show();
		ui->rightButton_1_5->show();
		break;
	case 2:
		m_bAdmin = false;
		ui->rightButton_1_1->hide();
		ui->rightButton_1_2->hide();
		ui->rightButton_1_3->hide();
		ui->rightButton_1_4->hide();
		ui->rightButton_1_5->hide();
		break;
	default:
		break;
	}

	SetCameraParameter();

}

void MainWindow::ReceiveProDlgInfo(int nIndex)
{
	switch (nIndex) {
	case 1:case 2:
		SearchProFiles();
		SetComBoBox();
		m_dProduct->ui->listWidget->clear();
		m_dProduct->ui->listWidget->addItems(file_names);
		break;
	case 3:
		// 切换ini
		ui->comboBox->setCurrentIndex(m_dProduct->m_nIndex);
		break;
	}
}


void MainWindow::WdPlcInfo(SIG_PLC nIndex)
{
	QString sIndex;
	QString sMsg;
	QString sSigPlc;
	m_dPlc->ui->label_2->setText("");
	if (!m_tPlc->m_worker->m_bPlcConnect)
	{
		return;
	}
	switch (nIndex)
	{
	case START:
		break;
	case STOP:
		break;
	case MOTOR1:
		break;
	case MOTOR2:
		break;
	case INCODERA:
		break;
	case INCODERB:
		break;
	case INSENSOR:
		break;
	case INPRESSSW1:
		break;
	case INPAUSESW:
		break;
	case INPRESSSW2:
		break;
	case ORIGINSW:
		break;
	case QPULSE1:
		m_tPlc->m_worker->writePlcInfo(QPULSE1, m_dPlc->m_nOut_2_1);
		break;
	case QPULSE2:
		m_tPlc->m_worker->writePlcInfo(QPULSE2, m_dPlc->m_nOut_4_1);
		break;
	case QDIRECTION1:
		m_tPlc->m_worker->writePlcInfo(QDIRECTION1, m_dPlc->m_nOut_2_2);
		break;
	case QDIRECTION2:
		m_tPlc->m_worker->writePlcInfo(QDIRECTION2, m_dPlc->m_nOut_4_2);
		break;
	case QENABLED1:
		m_tPlc->m_worker->writePlcInfo(QENABLED1, m_dPlc->m_nOut_2_3);
		break;
	case QENABLED2:
		m_tPlc->m_worker->writePlcInfo(QENABLED2, m_dPlc->m_nOut_4_3);
		break;
	case QGRAP1:
		m_tPlc->m_worker->writePlcInfo(QGRAP1, m_dPlc->m_nOut_1_1);
		break;
	case QGRAP2:
		m_tPlc->m_worker->writePlcInfo(QGRAP2, m_dPlc->m_nOut_1_2);
		break;
	case QGRAP3:
		m_tPlc->m_worker->writePlcInfo(QGRAP3, m_dPlc->m_nOut_1_3);
		break;
	case QGRAP4:
		m_tPlc->m_worker->writePlcInfo(QGRAP4, m_dPlc->m_nOut_1_4);
		break;
	case QNG1OUTAIR:
		m_tPlc->m_worker->writePlcInfo(QNG1OUTAIR, m_dPlc->m_nOut_3_1);
		break;
	case QNG2OUTAIR:
		m_tPlc->m_worker->writePlcInfo(QNG2OUTAIR, m_dPlc->m_nOut_3_2);
		break;
	case QOKOUTAIR:
		m_tPlc->m_worker->writePlcInfo(QOKOUTAIR, m_dPlc->m_nOut_3_3);
		break;
	case QLINE:
		m_tPlc->m_worker->writePlcInfo(QLINE, m_dPlc->m_nOut_6_3);
		break;
	case QPLATE:
		m_tPlc->m_worker->writePlcInfo(QPLATE, m_dPlc->m_nOut_6_1);
		break;
	case QINAIR:
		m_tPlc->m_worker->writePlcInfo(QINAIR, m_dPlc->m_nOut_6_2);
		break;
	case QVACUOINOUT:
		break;
	case QLIGHTG:
		m_tPlc->m_worker->writePlcInfo(QLIGHTG, m_dPlc->m_nOut_5_1);
		break;
	case QLIGHTY:
		m_tPlc->m_worker->writePlcInfo(QLIGHTY, m_dPlc->m_nOut_5_2);
		break;
	case QLIGHTR:
		m_tPlc->m_worker->writePlcInfo(QLIGHTR, m_dPlc->m_nOut_5_3);
		break;
	case QLIGHTVOX:
		break;
	case PLATEREV: // 0~15
		sIndex = m_dPlc->ui->lineEdit_11->text();

		m_tPlc->m_worker->writePlcInfo(PLATEREV, sIndex.toFloat());
		sSigPlc = QString::fromLocal8Bit("玻璃盘转速");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2 r/min").arg(sSigPlc).arg(sIndex);

		break;
	case MOTORRATION1: // 0~50
		sIndex = m_dPlc->ui->lineEdit_12->text();
		m_tPlc->m_worker->writePlcInfo(MOTORRATION1, sIndex.toFloat());
		sSigPlc = QString::fromLocal8Bit("玻璃盘转比");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case MOTORRATION2: // 0~20
		sIndex = m_dPlc->ui->lineEdit_13->text();
		m_tPlc->m_worker->writePlcInfo(MOTORRATION2, sIndex.toFloat());
		sSigPlc = QString::fromLocal8Bit("上料转比");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case OPTICAL:
		break;
	case POSITION_1:
		sIndex = m_dPlc->ui->lineEdit_5->text();
		m_tPlc->m_worker->writePlcInfo(POSITION_1, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("相机1位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case POSITION_2:
		sIndex = m_dPlc->ui->lineEdit_6->text();
		m_tPlc->m_worker->writePlcInfo(POSITION_2, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("相机2位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case POSITION_3:
		sIndex = m_dPlc->ui->lineEdit_7->text();
		m_tPlc->m_worker->writePlcInfo(POSITION_3, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("相机3位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case POSITION_4:
		sIndex = m_dPlc->ui->lineEdit_8->text();
		m_tPlc->m_worker->writePlcInfo(POSITION_4, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("相机4位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case POSITION_NG1:
		sIndex = m_dPlc->ui->lineEdit_2->text();
		m_tPlc->m_worker->writePlcInfo(POSITION_NG1, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("NG1位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case POSITION_NG2:
		sIndex = m_dPlc->ui->lineEdit_3->text();
		m_tPlc->m_worker->writePlcInfo(POSITION_NG2, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("NG2位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case POSITION_OK:
		sIndex = m_dPlc->ui->lineEdit_4->text();
		m_tPlc->m_worker->writePlcInfo(POSITION_OK, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("OK位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case RESULT_NUMBER:
		break;
	case CAMERA_RESULT:
		break;
	case WARN:
		sIndex = m_dPlc->ui->lineEdit_14->text();
		m_tPlc->m_worker->writePlcInfo(WARN, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("报警时间");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2 s").arg(sSigPlc).arg(sIndex);
		break;
	case ERROR_TIME:
		sIndex = m_dPlc->ui->lineEdit_15->text();
		m_tPlc->m_worker->writePlcInfo(ERROR_TIME, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("倒料时间");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2 ms").arg(sSigPlc).arg(sIndex);
		break;
	case ERROR_SITE:
		sIndex = m_dPlc->ui->lineEdit_16->text();
		m_tPlc->m_worker->writePlcInfo(ERROR_SITE, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("倒料位置");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2").arg(sSigPlc).arg(sIndex);
		break;
	case STOP_TIME:
		sIndex = m_dPlc->ui->lineEdit_17->text();
		m_tPlc->m_worker->writePlcInfo(STOP_TIME, sIndex.toInt());
		sSigPlc = QString::fromLocal8Bit("停机延时时间");
		sMsg = QString::fromLocal8Bit("成功：修改%1为：%2 s").arg(sSigPlc).arg(sIndex);
		break;
	default:
		break;
	}
	m_dPlc->ui->label_2->setText(sMsg);
}

void MainWindow::RdPlcInfo(SIG_PLC nIndex) {
	QString sSigPlc = "";
	QString sMsg = "";
	m_dPlc->ui->label_2->setText("");
	switch (nIndex)
	{
	case START:
		break;
	case STOP:
		break;
	case MOTOR1:
		break;
	case MOTOR2:
		break;
	case INCODERA:
		break;
	case INCODERB:
		break;
	case INSENSOR:
		break;
	case INPRESSSW1:
		break;
	case INPAUSESW:
		break;
	case INPRESSSW2:
		break;
	case ORIGINSW:
		break;
	case QPULSE1:
		break;
	case QPULSE2:
		break;
	case QDIRECTION1:
		break;
	case QDIRECTION2:
		break;
	case QENABLED1:
		break;
	case QENABLED2:
		break;
	case QGRAP1:
		break;
	case QGRAP2:
		break;
	case QGRAP3:
		break;
	case QGRAP4:
		break;
	case QNG1OUTAIR:
		break;
	case QNG2OUTAIR:
		break;
	case QOKOUTAIR:
		break;
	case QLINE:
		break;
	case QPLATE:
		break;
	case QINAIR:
		break;
	case QVACUOINOUT:
		break;
	case QLIGHTG:
		break;
	case QLIGHTY:
		break;
	case QLIGHTR:
		break;
	case QLIGHTVOX:
		break;
	case PLATEREV: // 0~15
		m_tPlc->m_worker->readPlcInfo(PLATEREV);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("玻璃盘转速");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2 r/min").arg(sSigPlc).arg(nPlcInfo.PlateRev);
		break;
	case MOTORRATION1: // 0~50
		m_tPlc->m_worker->readPlcInfo(MOTORRATION1);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("玻璃盘转比");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.MotorRation1);
		break;
	case MOTORRATION2: // 0~20
		m_tPlc->m_worker->readPlcInfo(MOTORRATION2);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("上料转比");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.MotorRation2);
		break;
	case OPTICAL:
		GetPlcState();
		break;
	case POSITION_1:
		m_tPlc->m_worker->readPlcInfo(POSITION_1);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("相机1位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Position_1);
		break;
	case POSITION_2:
		m_tPlc->m_worker->readPlcInfo(POSITION_2);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("相机2位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Position_2);
		break;
	case POSITION_3:
		m_tPlc->m_worker->readPlcInfo(POSITION_3);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("相机3位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Position_3);
		break;
	case POSITION_4:
		m_tPlc->m_worker->readPlcInfo(POSITION_4);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("相机4位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Position_4);
		break;
	case POSITION_NG1:
		m_tPlc->m_worker->readPlcInfo(POSITION_NG1);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("NG1位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Position_NG1);
		break;
	case POSITION_NG2:
		m_tPlc->m_worker->readPlcInfo(POSITION_NG2);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("NG2位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Position_NG2);
		break;
	case POSITION_OK:
		m_tPlc->m_worker->readPlcInfo(POSITION_OK);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("OK位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Position_OK);
		break;
	case RESULT_NUMBER:
		break;
	case CAMERA_RESULT:
		break;
	case WARN:
		m_tPlc->m_worker->readPlcInfo(WARN);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("报警时间");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2 s").arg(sSigPlc).arg(nPlcInfo.Warn);
		break;
	case ERROR_TIME:
		m_tPlc->m_worker->readPlcInfo(ERROR_TIME);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("倒料时间");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2 ms").arg(sSigPlc).arg(nPlcInfo.Error_time);
		break;
	case ERROR_SITE:
		m_tPlc->m_worker->readPlcInfo(ERROR_SITE);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("倒料位置");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2").arg(sSigPlc).arg(nPlcInfo.Error_site);
		break;
	case STOP_TIME:
		m_tPlc->m_worker->readPlcInfo(STOP_TIME);
		nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;

		sSigPlc = QString::fromLocal8Bit("停机延时时间");
		sMsg = QString::fromLocal8Bit("当前：%1为：%2 s").arg(sSigPlc).arg(nPlcInfo.Stop_time);
		break;
	default:
		break;
	}
	m_dPlc->ui->label_2->setText(sMsg);
}

void MainWindow::GetPlcState() {
	qDebug() << "GetPlcState";
	m_tPlc->m_worker->readPlcInfo(OPTICAL);
	nPlcInfo = m_tPlc->m_worker->m_coPlcInfo;
	m_dPlc->ui->lineEdit_1->setText(QString::number(nPlcInfo.Optical));
}

QString MainWindow::GetTime() {
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss");
	return current_date;
}
void MainWindow::SetAlgModel() {
	m_tAlg1->m_worker->m_nModel = 0;
	m_tAlg2->m_worker->m_nModel = 0;
	m_tAlg3->m_worker->m_nModel = 0;
	m_tAlg4->m_worker->m_nModel = 0;


	// 相机拍摄数据置空（为了让其他调用不影响使用）
	m_tAlg1->m_worker->m_nResult = 0;
	m_tAlg2->m_worker->m_nResult = 0;
	m_tAlg3->m_worker->m_nResult = 0;
	m_tAlg4->m_worker->m_nResult = 0;
}
void MainWindow::SetHalconWindows() {

	m_tCamera1->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle1;
	m_tCamera2->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle2;
	m_tCamera3->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle3;
	m_tCamera4->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle4;
}

void MainWindow::ClearCameraData() {
	CameraInfo m_nClear;
	m_nCameraResult = 0;  // 总量
	m_nNG1 = 0;
	m_nNG2 = 0;
	m_nNG3 = 0;
	m_nOK = 0;

	m_nResult = 0; // 速率
	m_nCameraInfo1 = m_nClear;
	m_nCameraInfo2 = m_nClear;
	m_nCameraInfo3 = m_nClear;
	m_nCameraInfo4 = m_nClear;




	m_tAlg1->m_worker->m_nNum = 0;
	m_tAlg2->m_worker->m_nNum = 0;
	m_tAlg3->m_worker->m_nNum = 0;
	m_tAlg4->m_worker->m_nNum = 0;

	m_nSS = 0;
	m_nMM = 0;
	m_nHH = 0;

	m_nDelet = 0;
	m_sRunTime = "";

	ShowUI();
	SetRunTime();
	SaveToIni();
}
void MainWindow::ShowUI() {
	float goodRatio1 = 0;
	float goodRatio2 = 0;
	float goodRatio3 = 0;
	float goodRatio4 = 0;
	if (m_nCameraInfo1.checkSum)
	{
		goodRatio1 = float(m_nCameraInfo1.goodSum) / float(m_nCameraInfo1.checkSum);
	}
	if (m_nCameraInfo2.checkSum)
	{
		goodRatio2 = float(m_nCameraInfo2.goodSum) / float(m_nCameraInfo2.checkSum);
	}
	if (m_nCameraInfo3.checkSum)
	{
		goodRatio3 = float(m_nCameraInfo3.goodSum) / float(m_nCameraInfo3.checkSum);
	}
	if (m_nCameraInfo4.checkSum)
	{
		goodRatio4 = float(m_nCameraInfo4.goodSum) / float(m_nCameraInfo4.checkSum);
	}

	if (m_bStart)
	{
		ui->label_2->setText(QString::number(m_tAlg1->m_worker->m_nNum));
		ui->label_3->setText(QString::number(m_tAlg2->m_worker->m_nNum));
		ui->label_4->setText(QString::number(m_tAlg3->m_worker->m_nNum));
		ui->label_5->setText(QString::number(m_tAlg4->m_worker->m_nNum));
	}
		



	//ui->label_2->setText(QString::number(m_nCameraInfo1.checkSum));
	//ui->label_3->setText(QString::number(m_nCameraInfo2.checkSum));
	//ui->label_4->setText(QString::number(m_nCameraInfo3.checkSum));
	//ui->label_5->setText(QString::number(m_nCameraInfo4.checkSum));


	ui->label_6->setText(QString::number(m_nCameraInfo1.goodSum));
	ui->label_7->setText(QString::number(m_nCameraInfo2.goodSum));
	ui->label_8->setText(QString::number(m_nCameraInfo3.goodSum));
	ui->label_9->setText(QString::number(m_nCameraInfo4.goodSum));

	QString strLabel_10 = QString("%1%2").arg(QString::number(goodRatio1 * 100, 'f', 2)).arg("%");
	QString strLabel_11 = QString("%1%2").arg(QString::number(goodRatio2 * 100, 'f', 2)).arg("%");
	QString strLabel_12 = QString("%1%2").arg(QString::number(goodRatio3 * 100, 'f', 2)).arg("%");
	QString strLabel_13 = QString("%1%2").arg(QString::number(goodRatio4 * 100, 'f', 2)).arg("%");
	ui->label_10->setText(strLabel_10);
	ui->label_11->setText(strLabel_11);
	ui->label_12->setText(strLabel_12);
	ui->label_13->setText(strLabel_13);


	ui->label_14->setText(QString::number(m_nCameraInfo1.badSum));
	ui->label_15->setText(QString::number(m_nCameraInfo2.badSum));
	ui->label_16->setText(QString::number(m_nCameraInfo3.badSum));
	ui->label_17->setText(QString::number(m_nCameraInfo4.badSum));



	// 生产数据
	//总量
	ui->label_18->setText(QString::number(m_nCameraResult));
	// 速率
	ui->label_19->setText(strRev);
	// 不良数
	ui->label_20->setText(QString::number(m_nNG1 + m_nNG2));
	// 良品数
	ui->label_21->setText(QString::number(m_nOK));


	// 良品率
	float fGoodRation = 0;
	float fBadRation = 0;
	if (m_nCameraResult) {
		fGoodRation = float(m_nOK) / float(m_nCameraResult);
		fBadRation = 1 - float(m_nOK) / float(m_nCameraResult);
	}
	QString sGoodRation = QString("%1%2").arg(QString::number(fGoodRation * 100, 'f', 2)).arg("%");
	QString sBadRation = QString("%1%2").arg(QString::number(fBadRation * 100, 'f', 2)).arg("%");
	ui->label_22->setText(sGoodRation);
	// NG1
	ui->label_23->setText(QString::number(m_nNG1));
	// NG 2
	ui->label_24->setText(QString::number(m_nNG2));
	// OK
	ui->label_25->setText(QString::number(m_nOK));

	// 未检
	ui->label_28->setText(QString::number(m_nNG3));

	ui->label_26->setText(sBadRation);

	// cs数据写入（舍弃）


	//    m_csData.csGoodRation = sGoodRation;
	//    m_csData.csBadRation = sBadRation;
}
void MainWindow::on_rightButton_1_5_clicked()
{
	m_dProduct->ui->listWidget->clear();
	m_dProduct->ui->listWidget->addItems(file_names);
	m_dProduct->m_dProCreat->m_detection = m_detection;
	m_dProduct->show();
}

void MainWindow::on_clearBt_clicked()
{
	ClearCameraData();
}

void MainWindow::changeEvent(QEvent *event)
{
	if (event->type() == QEvent::WindowStateChange)
	{
		if (!isMinimized())
		{
			setAttribute(Qt::WA_Mapped);
		}
	}          QWidget::changeEvent(event);
}



void MainWindow::CreateCameraSaveFile()
{

	QDir dir;
	QString imgPath = QDir::currentPath() + "/Pic";
	QString CalPath = QDir::currentPath() + "/Cal";
	bool img = dir.exists(imgPath);
	bool cal = dir.exists(CalPath);
	if (!img)
	{
		dir.mkdir(imgPath);

		dir.mkdir(imgPath + "/ccd1");
		dir.mkdir(imgPath + "/ccd1/OK");
		dir.mkdir(imgPath + "/ccd1/NG");
		dir.mkdir(imgPath + "/ccd1/Abnormal");

		dir.mkdir(imgPath + "/ccd2");
		dir.mkdir(imgPath + "/ccd2/OK");
		dir.mkdir(imgPath + "/ccd2/NG");
		dir.mkdir(imgPath + "/ccd2/Abnormal");



		dir.mkdir(imgPath + "/ccd3");
		dir.mkdir(imgPath + "/ccd3/OK");
		dir.mkdir(imgPath + "/ccd3/NG");
		dir.mkdir(imgPath + "/ccd3/Abnormal");

		dir.mkdir(imgPath + "/ccd4");
		dir.mkdir(imgPath + "/ccd4/OK");
		dir.mkdir(imgPath + "/ccd4/NG");
		dir.mkdir(imgPath + "/ccd4/Abnormal");

	}
	if (!cal)
	{
		dir.mkdir(CalPath);
		dir.mkdir(CalPath + "/Cal_Cricle");

		dir.mkdir(CalPath + "/Cal_Cricle/Pic");
		dir.mkdir(CalPath + "/Cal_Length");
		dir.mkdir(CalPath + "/Cal_Length/Pic");
		QFile filename1(CalPath + "/Cal_Cricle/Cricle.txt");
		filename1.open(QIODevice::WriteOnly);
		filename1.close();
		QFile filename2(CalPath + "/Cal_Length/Length.txt");
		filename2.open(QIODevice::WriteOnly);
		filename2.close();
	}
}
void MainWindow::SetCameraParameter() {
	m_tAlg1->m_worker->m_cCam1 = m_detection.m_Cam1;
	m_tAlg2->m_worker->m_cCam2 = m_detection.m_Cam2;
	m_tAlg3->m_worker->m_cCam3 = m_detection.m_Cam3;
	m_tAlg4->m_worker->m_cCam4 = m_detection.m_Cam4;


	m_dDetection->m_detectionInfo.m_Cam1 = m_detection.m_Cam1;
	m_dDetection->m_detectionInfo.m_Cam2 = m_detection.m_Cam2;
	m_dDetection->m_detectionInfo.m_Cam3 = m_detection.m_Cam3;
	m_dDetection->m_detectionInfo.m_Cam4 = m_detection.m_Cam4;
}

void MainWindow::on_btCmSav_1_clicked()
{
	m_tAlg1->m_worker->nSave = 1 - m_tAlg1->m_worker->nSave;
	SetCameraParameter();
	ShowSwitchBt();
}


void MainWindow::on_btCmSav_2_clicked()
{
	m_tAlg2->m_worker->nSave = 1 - m_tAlg2->m_worker->nSave;
	SetCameraParameter();
	ShowSwitchBt();
}

void MainWindow::on_btCmSav_3_clicked()
{
	m_tAlg3->m_worker->nSave = 1 - m_tAlg3->m_worker->nSave;
	SetCameraParameter();
	ShowSwitchBt();
}

void MainWindow::on_btCmSav_4_clicked()
{
	m_tAlg4->m_worker->nSave = 1 - m_tAlg4->m_worker->nSave;
	SetCameraParameter();
	ShowSwitchBt();
}
void MainWindow::ShowSwitchBt()
{
	if (m_tAlg1->m_worker->nSave) {
		ui->btCmSav_1->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmSav_1->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}

	if (m_tAlg2->m_worker->nSave) {
		ui->btCmSav_2->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmSav_2->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}

	if (m_tAlg3->m_worker->nSave) {
		ui->btCmSav_3->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmSav_3->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}

	if (m_tAlg4->m_worker->nSave) {
		ui->btCmSav_4->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmSav_4->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}
}

void MainWindow::AlgSwitchBt()
{
	if (m_nAlgSw1) {
		ui->btCmAlg_1->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmAlg_1->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}

	if (m_nAlgSw2) {
		ui->btCmAlg_2->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmAlg_2->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}

	if (m_nAlgSw3) {
		ui->btCmAlg_3->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmAlg_3->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}

	if (m_nAlgSw4) {
		ui->btCmAlg_4->setStyleSheet("border-image: url(:/png/imge/main/btncheckon.png);");
	}
	else {
		ui->btCmAlg_4->setStyleSheet("border-image: url(:/png/imge/main/btncheckoff.png);");
	}
}

void MainWindow::SaveToIni()
{

	QString manufacturePath = "Config/manufacture.ini";
	QSettings manufactureIniRead(manufacturePath, QSettings::IniFormat);
	manufactureIniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

	m_strName = file_names[m_nProductNum];
	manufactureIniRead.setValue("ProductNum/Num", m_nProductNum);
	manufactureIniRead.setValue("ProductNum/strName", m_strName);

	int* nCmData1 = &m_nCameraInfo1.checkSum;
	int* nCmData2 = &m_nCameraInfo2.checkSum;
	int* nCmData3 = &m_nCameraInfo3.checkSum;
	int* nCmData4 = &m_nCameraInfo4.checkSum;
	QString cm_fileName;
	for (int i = 0; i < 4; i++) {
		for (int y = 0; y < 3; y++) {
			cm_fileName =
				QString("CM%1%2%3")
				.arg(i + 1).arg("/parame").arg(y);
			switch (i) {
			case 0:
				manufactureIniRead.setValue(cm_fileName, *nCmData1);
				nCmData1++;
				break;
			case 1:
				manufactureIniRead.setValue(cm_fileName, *nCmData2);
				nCmData2++;
				break;
			case 2:
				manufactureIniRead.setValue(cm_fileName, *nCmData3);
				nCmData3++;
				break;
			case 3:
				manufactureIniRead.setValue(cm_fileName, *nCmData4);
				nCmData4++;
				break;
			}
		}
	}
	manufactureIniRead.setValue("SUM/parame0", m_nNG1);
	manufactureIniRead.setValue("SUM/parame1", m_nNG2);
	manufactureIniRead.setValue("SUM/parame2", m_nOK);
	manufactureIniRead.setValue("SUM/parame3", m_nCameraResult);
	manufactureIniRead.setValue("SUM/parame4", m_nSS);
	manufactureIniRead.setValue("SUM/parame5", m_nMM);
	manufactureIniRead.setValue("SUM/parame6", m_nHH);

	manufactureIniRead.setValue("SUM/parame7", m_nNG3);
}




void MainWindow::on_comboBox_currentIndexChanged(int index)
{
	if (index < 0) {
		return;
	}

	if (!bProSave)
	{
		bProSave = true;
		return;
	}
	m_nProductNum = index;
	RdProductIni(file_list[index]);
}

void MainWindow::on_btCmAlg_1_clicked()
{
	m_nAlgSw1 = 1 - m_nAlgSw1;
	AlgSwitchBt();
}

void MainWindow::on_btCmAlg_2_clicked()
{
	m_nAlgSw2 = 1 - m_nAlgSw2;
	AlgSwitchBt();
}

void MainWindow::on_btCmAlg_3_clicked()
{
	m_nAlgSw3 = 1 - m_nAlgSw3;
	AlgSwitchBt();
}

void MainWindow::on_btCmAlg_4_clicked()
{
	m_nAlgSw4 = 1 - m_nAlgSw4;
	AlgSwitchBt();
}

void MainWindow::on_OKAndNGBt_clicked()
{
	//0 NG  1 异常  2 All

	m_nOKAndNG++;
	if (m_nOKAndNG == 4) {
		m_nOKAndNG = 0;
	}
	switch (m_nOKAndNG) {
	case 0:
		ui->OKAndNGBt->setText("NG");
		break;
	case 1:
		ui->OKAndNGBt->setText(QString::fromLocal8Bit("异常"));
		break;
	case 2:
		ui->OKAndNGBt->setText(QString::fromLocal8Bit("NG|异常"));
		break;
	case 3:
		ui->OKAndNGBt->setText("All");
		break;
	}


	m_tSaveImg1->m_worker->m_nSaveType = m_nOKAndNG;
	m_tSaveImg2->m_worker->m_nSaveType = m_nOKAndNG;
	m_tSaveImg3->m_worker->m_nSaveType = m_nOKAndNG;
	m_tSaveImg4->m_worker->m_nSaveType = m_nOKAndNG;
}

void MainWindow::RdManufactureIni()
{



	// 获取上一次程序运行后的数据


	QString manufacturePath = "Config/manufacture.ini";
	QSettings manufactureIniRead(manufacturePath, QSettings::IniFormat);
	manufactureIniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));
	int* nCmData1 = &m_nCameraInfo1.checkSum;
	int* nCmData2 = &m_nCameraInfo2.checkSum;
	int* nCmData3 = &m_nCameraInfo3.checkSum;
	int* nCmData4 = &m_nCameraInfo4.checkSum;
	QString cm_fileName;
	for (int i = 0; i < 4; i++) {
		for (int y = 0; y < 3; y++) {
			cm_fileName =
				QString("CM%1%2%3")
				.arg(i + 1).arg("/parame").arg(y);
			switch (i) {
			case 0:
				*nCmData1 = manufactureIniRead.value(cm_fileName).toInt();
				nCmData1++;
				break;
			case 1:
				*nCmData2 = manufactureIniRead.value(cm_fileName).toInt();
				nCmData2++;
				break;
			case 2:
				*nCmData3 = manufactureIniRead.value(cm_fileName).toInt();
				nCmData3++;
				break;
			case 3:
				*nCmData4 = manufactureIniRead.value(cm_fileName).toInt();
				nCmData4++;
				break;
			}
		}
	}


	// 获取产品类型
	m_nProductNum = manufactureIniRead.value("ProductNum/Num").toInt();
	m_strName = manufactureIniRead.value("ProductNum/strName").toString();
	m_nNG1 = manufactureIniRead.value("SUM/parame0").toInt();
	m_nNG2 = manufactureIniRead.value("SUM/parame1").toInt();
	m_nOK = manufactureIniRead.value("SUM/parame2").toInt();
	m_nCameraResult = manufactureIniRead.value("SUM/parame3").toInt();

	m_nSS = manufactureIniRead.value("SUM/parame4").toInt();
	m_nMM = manufactureIniRead.value("SUM/parame5").toInt();
	m_nHH = manufactureIniRead.value("SUM/parame6").toInt();

	m_nNG3 = manufactureIniRead.value("SUM/parame7").toInt();
	ShowUI();
	SetRunTime();
}
